It is sometimes useful to be able to talk in terms of a pointer which points nowhere. Note that when you create a pointer it actually points nowhere useful and you must set it to a proper value before you use it. A null pointer is different, it is a pointer which has been set to point to the "official" version of nowhere.

You would use null in a situation where you ask a function to find something and it isn't there. The searching function would return a NULL, and your program could test for this. Your version of C should have a definition of the NULL pointer as part of its standard libraries:

/* is this pointer null? */
if ( ptr == NULL ) 

In real life the null pointer is usually the very bottom of memory. In the diagram on the right you can see that ptr1 and NULL point at the same location, so you can regard ptr1 as a null pointer.

Note that there is a nasty sting in the tail of this one. If you de-reference the null pointer C will not stop you, but instead will change the contents of a location at the bottom of memory. This is where some computers, including the IBM PC, keep all kinds of important stuff - so you should be very careful to make sure that the pointer points somewhere sensible before you de-reference it!

 


here, ptr1 is acting as a null pointer